﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAD_SiS.Models;

namespace SAD_SiS.Controllers
{
    public class CartController : Controller
    {
        //
        // GET: /Cart/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddToCart(FormCollection form)
        {
            Cart cart = GetCart();
            V_Product b;
            int productID = Convert.ToInt16(form["pid"]);
            int quantity = Convert.ToInt16(form["quantity"]);
            using (var dbContext = new SADEntities())
            {
                b = (from c in dbContext.V_Product
                     where c.ProductID == productID
                     select c).SingleOrDefault();

            }
            if (b == null)
            {
                return Json(new { Success = false });
            }

            // lay cac thuoc tinh dong
            int belongCate = (int)b.CateID;
            List<PropertyType> details;
            List<PairValue> lstOfValues = new List<PairValue>();
            using (var dbContext = new SADEntities())
            {
                details = (from c in dbContext.PropertyTypes
                           where c.CateID == belongCate && c.IsSelect == true
                           orderby c.TypeID ascending
                           select c).ToList();

                for (int i = 0; i < details.Count; i++)
                {
                    PairValue value = new PairValue();
                    value.name = details[i].PropertyName;
                    value.id = Convert.ToInt16(form["slt-" + details[i].TypeID]);
                    var val = (from c in dbContext.ProductDetails where c.DetailID == value.id select c).Single().Value;
                    value.value = val;
                    lstOfValues.Add(value);
                }
            }
            GeneralProduct gp = new GeneralProduct();
            gp.P = b;
            gp.Details = lstOfValues;
            cart.AddBook(gp, quantity);
            return Json(new { Success = true });
        }

        [HttpPost]
        public ActionResult RemoveFromCart(int productID,int locationID)
        {
            // xoa hang khoi gio
            Cart cart = GetCart();
            // tim hang
            V_Product b;
            using (var dbContext = new SADEntities())
            {
                b = (from c in dbContext.V_Product
                     where c.ProductID == productID
                     select c).SingleOrDefault();

            }
            if (b == null)
            {
                return Json(new { Success = false, BName = productID });
            }
            // co hang thi xoa
            cart.UpdateQuantity(productID,locationID, 0);
            // tinh lai tong cong
            var sum = cart.GetTotal();

            return Json(new { Success = true, BName = b.Name, Sum = sum, End = cart.IsEmpty() });
        }

        [HttpPost]
        public ActionResult UpdateCart(int productID, int locationID,int quantity)
        {
            // sua hang trong gio
            Cart cart = GetCart();
            // tim hang
            V_Product b;
            using (var dbContext = new SADEntities())
            {
                b = (from c in dbContext.V_Product
                     where c.ProductID == productID
                     select c).SingleOrDefault();

            }
            if (b == null)
            {
                return Json(new { Success = false, BName = productID });
            }
            // co hang thi sua so luong
            cart.UpdateQuantity(productID,locationID, quantity);

            // tinh lai tong cong
            var sum = cart.GetTotal();
            var tempSum = b.SellPrice * quantity;

            return Json(new { Success = true, Sum = sum, Temp = tempSum });

        }


        public ActionResult GetCartButton()
        {
            return PartialView("CartButton");
        }

        private Cart GetCart()
        {
            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"] = cart;
            }
            return cart;
        }

        public ActionResult Cart()
        {
            return View();
        }
    }
}
